<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
       .slider-bar {
         width: 85px;
         height: 100px;
         background-color: red;
         position: absolute;
         right: 0;
         bottom: 310px;
         z-index: 999;
         display: block;
       }
       .w{
         width: 1180px;
         margin: 0 auto;
       }
       .header{
       
         height: 100px;
         background-color: rgb(211, 104, 197);
       }
       .banner{
         height: 300px;
         background-color: rgb(94, 184, 139);
         margin-top: 20px;
         margin-bottom: 20px;
       }
       .main{
         height: 1000px;
         background-color: rgb(181, 212, 66);
       }
    </style>
</head>
<body>

    <div class="slider-bar">
        <span>返回顶部</span>
    </div>
    <div class="header w">头部区域</div>
    <div class="banner w">banner区域</div>
    <div class="main w">主体部分</div>


    <script>
    let sliderBar= document.querySelector(".slider-bar")
     let banner= document.querySelector(".banner")

     document.addEventListener('scroll', function(){
        console.log(window.pageYOffset);
        if(window.pageYOffset >=banner.offsetTop){
            sliderBar.style.position = 'fixed';
        }else{
           sliderBar.style.position = 'absolute';
        }
     })

      console.log();


    </script>
</body>
</html>